//
//  DicHeaderView.m
//  划词显示
//
//  Created by 盛超 on 2018/8/16.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "DicHeaderView.h"
#import "SCDict.h"
#import "DicImageBtn.h"
#import "NSString+DicUtils.h"
#import "UIView+DicUtils.h"
#import "BKDictAudioPlayer.h"

@interface DicHeaderView ()<BKAudioPlayerDelegate>

@property (nonatomic,strong) UILabel *wordNameLabel;
@property (nonatomic,strong) DicImageBtn *unPtextBtn;
@property (nonatomic,strong) DicImageBtn *usPtextBtn;
@property (nonatomic,strong) BKDictAudioPlayer *player;

@property (nonatomic,assign) BOOL isMean;//释义
@property (nonatomic,assign) BOOL isColl;//搭配
@property (nonatomic,assign) BOOL isDisc;//辨析
@property (nonatomic,assign) BOOL isClass;//经典


@end

@implementation DicHeaderView

- (instancetype)initWithFrame:(CGRect)frame ShowRecord:(BOOL)show{
    if (self = [super initWithFrame:frame]) {
        
        BKDictAudioPlayer *player = [BKDictAudioPlayer sharedMPlayer];
        self.player = player;
        player.delegateM = self;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeAllPlayer) name:@"LSDictColsePlayer" object:nil];
    }
    
    return self;
}

#pragma mark - 按钮方法
- (void)upDownAction:(DicImageBtn*)sender {
    sender.becomeHightlightly = YES;
}

- (void)upInsideAction:(DicImageBtn*)sender {
    sender.becomeHightlightly = NO;
    if (sender.tag==2002) {//播放英式发音，暂停美式发音
        [self.usPtextBtn stopPlayAnimation];
       // self.player.url = _model.unPVoice;
        [self.player playerUrl:_model.unPVoice seekTotime:0 rateValue:1];
        [self.player playerPlay];
        
    } else if (sender.tag==2003) {//播放美式发音，暂停音式发音
        [self.unPtextBtn stopPlayAnimation];
       // self.player.url = _model.usPVoice;
        [self.player playerUrl:_model.usPVoice seekTotime:0 rateValue:1];
        [self.player playerPlay];
    }
}

#pragma mark - 全部暂停播放
- (void)stopPlay {
    [self.usPtextBtn stopPlayAnimation];
    [self.unPtextBtn stopPlayAnimation];
    [self.player closePlayer];
}

#pragma mark - 更新控件数据
- (void)setModel:(ModelDic *)model {
    _model = model;
    [self updateControlData];
}

- (void)updateControlData {
  
    UILabel *wordNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, self.frame.size.width-20, 40)];
    self.wordNameLabel = wordNameLabel;
    wordNameLabel.numberOfLines = 0;
    wordNameLabel.font = [UIFont boldSystemFontOfSize:15];
    [self addSubview:wordNameLabel];
    self.wordNameLabel.text = _model.cwName;
    //英式发音
    [self addSubview:self.unPtextBtn];
    //美式发音
    [self addSubview:self.usPtextBtn];

    self.unPtextBtn.title = [NSString stringWithFormat:@"英%@",_model.unPText];
    self.usPtextBtn.title = [NSString stringWithFormat:@"美%@",_model.usPText];
    
    [self addButton];
}

- (void)addButton {
    _meanBtn = [[UIButton alloc] init];
    _meanBtn.tag = 2001;
    _meanBtn.frame = CGRectMake(10,self.height-16,56, 13);
 //   _meanBtn.backgroundColor = [UIColor redColor];
    _meanBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _meanBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_meanBtn setTitleColor:[UIColor colorWithRed:3.0/255 green:134.0/255 blue:235.0/255 alpha:1] forState:UIControlStateNormal];
    [_meanBtn setTitle:@"释义例句" forState:UIControlStateNormal];
    [_meanBtn addTarget:self action:@selector(buttonAtion:) forControlEvents:UIControlEventTouchUpInside];
    
    _collBtn = [[UIButton alloc] init];
    _collBtn.tag = 2002;
    _collBtn.frame = CGRectMake(_meanBtn.x+_meanBtn.width+10,self.height-16, 56, 13);
  //  _collBtn.backgroundColor = [UIColor redColor];
    _collBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _collBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_collBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_collBtn setTitle:@"常用搭配" forState:UIControlStateNormal];
    [_collBtn addTarget:self action:@selector(buttonAtion:) forControlEvents:UIControlEventTouchUpInside];

    _discBtn = [[UIButton alloc] init];
    _discBtn.tag = 2003;
    _discBtn.frame = CGRectMake(_collBtn.x+_collBtn.width+10,self.height-16, 56, 13);
   // _discBtn.backgroundColor = [UIColor redColor];
    _discBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _discBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_discBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_discBtn setTitle:@"辨析" forState:UIControlStateNormal];
    [_discBtn addTarget:self action:@selector(buttonAtion:) forControlEvents:UIControlEventTouchUpInside];
    
    _classBtn = [[UIButton alloc] init];
    _classBtn.tag = 2004;
    _classBtn.frame = CGRectMake(_discBtn.x+_discBtn.width+10,self.height-16, 56, 13);
   // _classBtn.backgroundColor = [UIColor redColor];
    _classBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _classBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_classBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_classBtn setTitle:@"经典例句" forState:UIControlStateNormal];
    [_classBtn addTarget:self action:@selector(buttonAtion:) forControlEvents:UIControlEventTouchUpInside];
    
    _lineImageView = [[UIImageView alloc] init];
    _lineImageView.image = [UIImage imageNamed:@"LSDict.bundle/选项卡选中下横线"];
    _lineImageView.frame = CGRectMake(10,self.height-2,56, 2);

    [self addSubview:_lineImageView];
    [self addSubview:_meanBtn];
    [self addSubview:_collBtn];
    [self addSubview:_discBtn];
    [self addSubview:_classBtn];
    
    for(NSInteger i = 0 ; i<_model.cxCollection.count;i++) {
        cx *cxModel = _model.cxCollection[i];
        if (cxModel.meanCollection.count>0) {
            _isMean = YES;
        }
        if (cxModel.CollCollection.count>0) {
            _isColl = YES;
        }
        if (cxModel.DiscCollection.count>0) {
            _isDisc = YES;
        }
        if (cxModel.ClassCollection.count>0) {
            _isClass = YES;
        }
    }
    if (!_isMean) {
        [_meanBtn removeFromSuperview];
        _meanBtn = nil;
        _collBtn.frame = CGRectMake(10,self.height-16,56, 13);
        _discBtn.frame = CGRectMake(_collBtn.x+_collBtn.width+10,self.height-16, 56, 13);
        _classBtn.frame = CGRectMake(_discBtn.x+_discBtn.width+10,self.height-16, 56, 13);
    }
    if (!_isColl) {
        [_collBtn removeFromSuperview];
        _collBtn = nil;
        if (_isMean) {
            _discBtn.frame = CGRectMake(_meanBtn.x+_meanBtn.width+10,self.height-16, 56, 13);
        } else {
            _discBtn.frame = CGRectMake(10,self.height-16,56, 13);
        }
        _classBtn.frame = CGRectMake(_discBtn.x+_discBtn.width+10,self.height-16, 56, 13);
    }
    if (!_isDisc) {
        [_discBtn removeFromSuperview];
        _discBtn = nil;
        
        if ((_isMean && _isColl) || (!_isMean && _isColl)) {
             _classBtn.frame = CGRectMake(_collBtn.x+_collBtn.width+10,self.height-16, 56, 13);
        }
        if (_isMean && !_isColl) {
             _classBtn.frame = CGRectMake(_meanBtn.x+_meanBtn.width+10,self.height-16, 56, 13);
        }
        if (!_isMean && !_isColl) {
            _classBtn.frame = CGRectMake(10,self.height-16,56, 13);
        }
    }
    if (!_isClass) {
        [_classBtn removeFromSuperview];
        _classBtn = nil;
    }
    
    NSArray *array = @[@(_isMean),@(_isColl),@(_isDisc),@(_isClass)];
    if (self.delegate && [self.delegate respondsToSelector:@selector(showBtns:)]) {
        [self.delegate showBtns:array];
    }
}

#pragma mark - BKAudioPlayerDelegate
- (void)audioPlayEnd {
    [self stopPlay];
}

- (void)audioUpdateWith:(float)time Totaltime:(float)totalTime {
    
}

#pragma mark - 懒加载
- (DicImageBtn *)unPtextBtn {
    if (!_unPtextBtn) {
        _unPtextBtn = [[DicImageBtn alloc] initWithFrame:CGRectMake(10,60,100, 20) title:@"" image:[UIImage imageNamed:@"LSDict.bundle/句子读音图标"] ImagePosition:DicImgInRight];
        _unPtextBtn.tag = 2002;
        _unPtextBtn.animationImages = @[[UIImage imageNamed:@"LSDict.bundle/句子读音图标"],[UIImage imageNamed:@"LSDict.bundle/句子读音图标"],[UIImage imageNamed:@"LSDict.bundle/句子读音图标"]];

        [_unPtextBtn addTarget:self action:@selector(upDownAction:) forControlEvents:UIControlEventTouchDown];
        [_unPtextBtn addTarget:self action:@selector(upInsideAction:) forControlEvents:UIControlEventTouchUpInside];

    }
    
    return _unPtextBtn;
}

- (DicImageBtn *)usPtextBtn {
    if (!_usPtextBtn) {
        _usPtextBtn = [[DicImageBtn alloc] initWithFrame:CGRectMake(self.unPtextBtn.frame.origin.x,self.unPtextBtn.frame.origin.y+self.unPtextBtn.frame.size.height+10,100, 20) title:@"" image:[UIImage imageNamed:@"LSDict.bundle/句子读音图标"] ImagePosition:DicImgInRight];
        _usPtextBtn.tag = 2003;
        _usPtextBtn.animationImages = @[[UIImage imageNamed:@"LSDict.bundle/句子读音图标"],[UIImage imageNamed:@"LSDict.bundle/句子读音图标"],[UIImage imageNamed:@"LSDict.bundle/句子读音图标"]];
        [_usPtextBtn addTarget:self action:@selector(upDownAction:) forControlEvents:UIControlEventTouchDown];
        [_usPtextBtn addTarget:self action:@selector(upInsideAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _usPtextBtn;
}

#pragma mark - 按钮点击
- (void)buttonAtion:(UIButton*)sender {
    [UIView animateWithDuration:0.2 animations:^{
        
        NSInteger index= 0;//判断按钮滚动的位置
        if (sender.tag==2001) {
            [_meanBtn setTitleColor:[UIColor colorWithRed:3.0/255 green:134.0/255 blue:235.0/255 alpha:1] forState:UIControlStateNormal];
            [_collBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_discBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_classBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            _lineImageView.x = _meanBtn.x;
            
            index = 0;
            
        } else if (sender.tag == 2002) {
            [_collBtn setTitleColor:[UIColor colorWithRed:3.0/255 green:134.0/255 blue:235.0/255 alpha:1] forState:UIControlStateNormal];
            [_meanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_discBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_classBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            _lineImageView.x = _collBtn.x;
            if (_isMean) {
                index = 1;
            } else
            {
                index = 0;
            }
            
        } else if (sender.tag == 2003) {
            [_discBtn setTitleColor:[UIColor colorWithRed:3.0/255 green:134.0/255 blue:235.0/255 alpha:1] forState:UIControlStateNormal];
            [_meanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_collBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_classBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            _lineImageView.x =  _discBtn.x;
            
            if(_isMean && _isColl) {
                index = 2;
            }
            if (_isMean && !_isColl) {
                index = 1;
            }
            if (!_isMean && !_isColl) {
                index = 0;
            }
            
        } else if (sender.tag == 2004) {
            [_classBtn setTitleColor:[UIColor colorWithRed:3.0/255 green:134.0/255 blue:235.0/255 alpha:1] forState:UIControlStateNormal];
            [_meanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_collBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [_discBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            _lineImageView.x = _classBtn.x;
            
            if (_isMean && _isColl && _isDisc) {
                index = 3;
            }
            if ((_isMean && _isColl && !_isDisc) || (_isMean && !_isColl && _isDisc) || (!_isMean && _isColl && _isDisc)) {
                index = 2;
            }
            if ((_isMean && !_isColl && !_isDisc) || (!_isMean && !_isColl && _isDisc) || (!_isMean && _isColl && !_isDisc)) {
                index = 1;
            }
            if (!_isMean && !_isColl && !_isDisc) {
                index = 0;
            }
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(selectBtn:ScrollToIndex:)]) {
            [self.delegate selectBtn:sender ScrollToIndex:index];
        }
    }];
    
}

- (void)closeAllPlayer {
    [self.player closePlayer];
}

@end

